﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

public partial class Controls_CatList : System.Web.UI.UserControl
{
    string categoryID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        categoryID = Request.QueryString["CategoryID"];
        if (!IsPostBack)
            PopulateControl();
    }

    private void PopulateControl()
    {
        repCatList.DataSource = Category.All();
        repCatList.DataBind();
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        repCatList.ItemDataBound += new RepeaterItemEventHandler(repCatList_ItemDataBound);
    }

    void repCatList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem
            || e.Item.ItemType == ListItemType.Item)
        {
            Category data = e.Item.DataItem as Category;
            HyperLink lnkCat = (HyperLink)e.Item.FindControl("lnkCat");
            lnkCat.Text = data.Name;
            string id = data.CategoryID.ToString();
            lnkCat.NavigateUrl = Link.ToCategory(id);
            lnkCat.CssClass = id == categoryID ? "current" : "";
            lnkCat.ToolTip = data.Description;
        }
    }
}